<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\validate\PrizeValidate;
use app\model\Prize as PrizeModel;
use think\facade\Db;
use think\Request;
use app\exception\HttpExceptions;
class Prize extends AdminAuth
{


    /**
     * @param $id
     * @return \think\response\Json
     * 删除奖项
     */
    public function delete($id) {
        PrizeModel::destroy($id);
        return $this -> result();
    }
     /**
     * 添加修改奖项
     */
    public function save(Request $request) {
        $data = $request -> post();
        (new PrizeValidate()) -> checkParam($data);
        $prize = new PrizeModel();
        if(isset($data['id'])) {
            $prize -> update($data);
        } else {
            $prize->save($data);
        }
        return $this -> result();
    }

     /**
     * 获取奖项列表
     */
    public function list (Request $request) {
        $list = Db::name('prize')->select()->toArray();
        return $this->result(compact('list'));
    }

    /**
     * 一键抽奖
     */
    public function draw() {
        $count = Db::name('prize')->count('*');
        if($count < 1) {
            throw new HttpExceptions('没有设置奖项无法抽奖');
        }
        PrizeModel::draw();
        return $this->result();
    }

    public function prizeList(Request $request) {
        $page = $request->get('page');
        $size = $request->get('size');
        if(!is_numeric($page) || !is_numeric($size)) {
            throw new HttpExceptions('非法参数');
        }
        $list = PrizeModel::prizeList();
        return $this->result($list);
    }


}
